package common.entities.abst;


/**
 * @author Yoyo
 *
 */
public class AbstPersonEntity extends AbstPersistentEntity {

	private static int firstNameMinLength = 1;
	private static int firstNameMaxLength = 45;
	
	private static int lastNameMinLength = 1;
	private static int lastNameMaxLength = 45;
	
	private String firstName;
	private String lastName;
	
	private String personNumber;		//='(IV)' for example. like when we have multiple 'Will, Smith'
	
	
	/**
	 * This constructor will be used for entities that are fetched from the DB
	 * 
	 * @param id
	 * @param firstName
	 * @param lastName
	 */
	public AbstPersonEntity(int id, String firstName, String lastName) {
		super(id);
		
		this.firstName = fixEscapeCharacters(firstName);
		this.lastName = fixEscapeCharacters(lastName);
		this.personNumber = null;
	}
	
	/**
	 * This constructor will be used for new entities (i.e still weren't saved into DB)
	 * 
	 * @param firstName
	 * @param lastName
	 */
	public AbstPersonEntity(String firstName, String lastName) {
		super();
		
		this.firstName = fixEscapeCharacters(firstName);
		this.lastName = fixEscapeCharacters(lastName);
		this.personNumber = null;
	}
	
	public AbstPersonEntity() {
		this(null, null);
	}

	
	/**
	 * @return the firstName
	 */
	public String getFirstName() {
		return fixEscapeCharactersReverse(firstName);
	}
	
	public String escapedGetFirstName() {
		return firstName;
	}

	/**
	 * @return the lastName
	 */
	public String getLastName() {
		return fixEscapeCharactersReverse(lastName);
	}
	
	public String escapedGetLastName() {
		return lastName;
	}
	
	/**
	 * @param firstName the firstName to set
	 */
	public void setFirstName(String firstName) {
		this.firstName = fixEscapeCharacters(firstName);
	}

	/**
	 * @param lastName the lastName to set
	 */
	public void setLastName(String lastName) {
		this.lastName = fixEscapeCharacters(lastName);
	}

	/**
	 * @param personNumber the personNumber to set
	 */
	public void setPersonNumber(String personNumber) {
		this.personNumber = personNumber;
	}

	/**
	 * @return the personNumber
	 */
	public String getPersonNumber() {
		return personNumber;
	}
	
	/**
	 * @return the lastName - if the person has a number (like 'IV'), 
	 * 			then it will return the last name + the person number
	 */
	public String getLastNameWithPersonNumber() {
		if (personNumber == null)
			return this.getLastName();
		else
			return this.getLastName() + " " + personNumber;
	}
	
	/**
	 * Is used to check the restrictions of the entity. 
	 * @return true if it's fields violate the restrictions, and false otherwise
	 */
	public boolean isViolateRestrictions() {
		if(firstName == null || lastName == null)
			return true;
		else if(firstName.length() <= AbstPersonEntity.firstNameMinLength ||
				firstName.length() >= AbstPersonEntity.firstNameMaxLength)
			return true;
		else if(getLastNameWithPersonNumber().length() <= AbstPersonEntity.lastNameMinLength ||
				getLastNameWithPersonNumber().length() >= AbstPersonEntity.lastNameMaxLength)
			return true;
		
		return false;
	}
	
	/**
	 * Is used to validate the first name according to the DB constraints
	 * 
	 * @param firstName
	 * @throws IllegalArgumentException
	 */
	public static void validateFirstName(String firstName) throws IllegalArgumentException{
		if(firstName == null)
			throw new IllegalArgumentException("First name cannot be null");
		else if(firstName.length() < AbstPersonEntity.firstNameMinLength)
			throw new IllegalArgumentException(String.format("First name cannot have less than %s characters", AbstPersonEntity.firstNameMinLength));
		else if(firstName.length() > AbstPersonEntity.firstNameMaxLength)
			throw new IllegalArgumentException(String.format("First name cannot have more than %s characters", AbstPersonEntity.firstNameMaxLength));
	}
	
	/**
	 * Is used to validate the last name according to the DB constraints
	 * 
	 * @param lastName
	 * @throws IllegalArgumentException
	 */
	public static void validateLastName(String lastName) throws IllegalArgumentException{
		if(lastName == null)
			throw new IllegalArgumentException("Last name cannot be null");
		else if(lastName.length() < AbstPersonEntity.lastNameMinLength)
			throw new IllegalArgumentException(String.format("Last name cannot have less than %s characters", AbstPersonEntity.lastNameMinLength));
		else if(lastName.length() > AbstPersonEntity.lastNameMaxLength)
			throw new IllegalArgumentException(String.format("Last name cannot have more than %s characters", AbstPersonEntity.lastNameMaxLength));
	}
	
	
	@Override
	public boolean equals(Object obj) {
		AbstPersonEntity cmp = (AbstPersonEntity) obj;
		
		if(this.getFirstName() == null || this.getLastName() == null)
			return false;
		if(cmp.getFirstName() == null || cmp.getLastName() == null)
			return false;
		
		return ( (this.getFirstName().equals(cmp.getFirstName())) && 
				 (this.getLastName().equals(cmp.getLastName())) );
	}
	
}
